package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.cache.redis.service.IStrategyStatesVoService;
import cn.wolfcode.wolf2w.cache.redis.vo.StrategyStatisVo;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定义一个监听器,用来给数据初始化
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatesVoService ste;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println(".....................................");
        //这个方法是spring泳器运行完了才运行的
        //拿到所有的攻略
        List<Strategy> strategyList = strategyService.lists();
        //创建一个空的vo
        for (Strategy strategy : strategyList) {
            //判断vo是否存在,有了就不用初始化
            if (ste.isStrategyStatisVoExs(strategy.getId())){
                continue;
            }
            StrategyStatisVo vo=new StrategyStatisVo();
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
            ste.setStrategyStatisVo(vo);
        }
    }
}
